/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.capacity;

import gov.fema.hazus.bdc.shared.entity.Capacity;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class CapacityTextBoxKeyListener implements CapacityTextBoxKeyListenerInterface {

	private Capacity capacity;
	
	public CapacityTextBoxKeyListener(Capacity capacity)
	{
		this.capacity = capacity;
	}
	
	public void onFocus(Widget arg0) {
		// do nothing
	}

	public void onLostFocus(Widget arg0) {

		TextBox textBox = (TextBox) arg0;
		String boxName = textBox.getName();
		
		// get the form data as string, long or int
		String value = textBox.getText();
		Integer intValue = null;
		try {
			intValue = Integer.valueOf(value);
		} catch (NumberFormatException e) {
			// swallow
		}
		
		GWT.log("text box name=" + boxName, null);
		
		if( boxName.equals(Capacity.DAYTIME) )
		{			
			GWT.log( Capacity.DAYTIME + ": " + value, null);
			this.capacity.setDaytimeOccupants(intValue);
		} else if ( boxName.equals( Capacity.NIGHTTIME ) ) {
			GWT.log( Capacity.NIGHTTIME + ": " + value, null);
			this.capacity.setNighttimeOccupants(intValue);			
		} else if ( boxName.equals( Capacity.KITCHEN ) ) {
			GWT.log( Capacity.KITCHEN + ": " + value, null);
			this.capacity.setKitchen(value);
		} else if ( boxName.equals( Capacity.KITCHENCAPACITY ) ) {
			GWT.log( Capacity.KITCHENCAPACITY + ": " + value, null);
			this.capacity.setKitchenCapacity(intValue);
		} else if ( boxName.equals( Capacity.DINING ) ) {
			GWT.log( Capacity.DINING + ": " + value, null);
			this.capacity.setDining(value);
		} else if ( boxName.equals( Capacity.DININGCAPACITY ) ) {
			GWT.log( Capacity.DININGCAPACITY + ": " + value, null);
			this.capacity.setDiningCapacity(intValue);
		} else if ( boxName.equals( Capacity.SLEEPING ) ) {
			GWT.log( Capacity.SLEEPING + ": " + value, null);
			this.capacity.setSleeping(value);			
		} else if ( boxName.equals( Capacity.SLEEPINGCAPACITY ) ) {
			GWT.log( Capacity.SLEEPINGCAPACITY + ": " + value, null);
			this.capacity.setSleepingCapacity(intValue);
		} else if ( boxName.equals( Capacity.HOSPITALBEDS ) ) {
			GWT.log( Capacity.HOSPITALBEDS + ": " + value, null);
			this.capacity.setHospitalBeds(intValue);
		} else if ( boxName.equals( Capacity.VEHICLES ) ) {
			GWT.log( Capacity.VEHICLES + ": " + value, null);
			this.capacity.setVehicles(intValue);
		} else if ( boxName.equals( Capacity.HAZARDOUSMATERIALS ) ) {
			GWT.log( Capacity.HAZARDOUSMATERIALS + ": " + value, null);
			this.capacity.setHazardousMaterials(value);				
		} else {
			// do nothing
		}
		
	}

	public void updateCapacity(Capacity capacity) {
		this.capacity = capacity;
	}

}
